package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.AdChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;

@Service
@Slf4j
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        if(removeById(id)) {
            return ResponseResult.okResult(200,"删除成功！");
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
    }

    /**
     * 频道名称模糊查询，分页查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(AdChannelDto dto) {

        //检查参数
        dto.checkParam();

        //分页查询
        IPage page = new Page(dto.getPage(), dto.getSize());

        //构建查询条件
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(AdChannel::getName, dto.getName()).orderByDesc(AdChannel::getCreatedTime);
        if(dto.getStatus() != null) {
            lambdaQueryWrapper.eq(AdChannel::getStatus, dto.getStatus());
        }

        page = page(page, lambdaQueryWrapper);

        //结果返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int)page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 保存频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult saveOne(AdChannel adChannel) {
        if(adChannel == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        adChannel.setCreatedTime(new Date());
        if (saveOrUpdate(adChannel)) {
            return ResponseResult.okResult(200, "保存成功！");
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
    }

    /**
     * 修改频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult updateOne(AdChannel adChannel) {
        if(adChannel == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        adChannel.setCreatedTime(new Date());
        if (saveOrUpdate(adChannel)) {
            return ResponseResult.okResult(200, "修改成功！");
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
    }
}
